
	$(function(){
		//创建3个不同上传文件的不同url Map
		var fileUrl = {
				"songFile": window.url.UPLOAD_MP3,
				"imgFile" : window.url.UPLOAD_IMG,
				"lyrFile" : window.url.UPLOAD_LYR
				};
		var fileUploadStatus = {};
		
		initSongFileInput();
		initLyrFileInput();
		initImgFileInput();
		
		function initSongFileInput() {
			$("#songFile").fileinput({
		        showUpload : false,
		        showRemove : false,
		        showPreview: false,
		        language : 'zh',
		        dropZoneEnabled: false,
		        allowedPreviewTypes: ['audio'],
		        allowedFileTypes: ['audio'],
		        allowedFileExtensions:  ['mp3'],
		        maxFileSize : 15360,
		    }).on("filebatchselected", function(event, files) {
		    	$(this).prev("p").hide();
		    	var file = $(this)[0].files[0];
		    	var fileName = file.name;
		    	fillInput(fileName);
		    });
		}
		
		function initLyrFileInput() {
			$("#lyrFile").fileinput({
		        showUpload : false,
		        showRemove : false,
		        showPreview: false,
		        language : 'zh',
		        dropZoneEnabled: false,
		        maxFileSize : 100,
		    }).on("filebatchselected", function(event, files) {
		    	$(this).prev("p").hide();
		    });
		}
		
		function initImgFileInput() {
			$("#imgFile").fileinput({
		        showUpload : false,
		        showRemove : false,
		        //showPreview: false,
		        language : 'zh',
		        dropZoneEnabled: false,
		        allowedPreviewTypes: ['image'],
		        allowedFileTypes: ['image'],
		        allowedFileExtensions:  ['png','jpg','jpeg'],
		        maxFileSize : 2048,
		    }).on("filebatchselected", function(event, files) {
		    	$(this).prev("p").hide();
		    });
		}
		
		function fillInput(fileName) {
			  alert("fill")
			  $.ajax({
				  type: "post",
				  url: window.url.CONVERT,
				  data: {"fileName": fileName},
				  async: false,
				  success: function(data) {
					  $("#songName").val(data.songName);
					  $("#singer").val(data.singer);
				  }
			  });
		}
		
		$("#songSubmit").click(function(){
			if(!$("#songform").valid())
				return false;
			uploadFile();
			return false;
		})
		
		function uploadFile() {
			  
			  var fd = null;
			  var name = "";
			  var status = "";
			  var url = "";
			  var files = $(":file");
			  var hasError = false;
			  $.each(files, function(index, item){
			  	fd = new FormData();
			  	name = $(item).attr("name");
			  	status = fileUploadStatus[name];
			  	//如何当前文件已上传成功,则跳过，进行下一个文件上传
			  	if(status == "true") 
			  		return true;
			  	url = fileUrl[name];
			  	fd.append(name, item.files[0]);
			  	$.ajax({
						type: 'post',
						url: url,
						data: fd,
						async: false,  
					    cache: false,  
					    contentType: false,  
					    processData: false, 
						success: function(data){
							if(data.code == 0) {
								var name = $(item).attr("name");
								//alert(name);
								//$(item).next("div").html("");
								fileUploadStatus[name] = "true";
							}
							if(data.code == 1){
								//alert(data.message);
								var className = $(item).prev("p").attr("class");
								alert(className);
								$(item).prev("p").text(data.message).show();
								hasError = true;
							}
						}
					});
			  	if(hasError) {
			  		return false;
			  	}
			  });
			  if(!hasError) 
			  	songForm();
			}
			//服务端提交数据
			var songForm = function(){
				alert("songForm");
				alert(url.NEW_SONG);
				//json格式:{songName : '大王来巡山'，singer:'贾乃亮'}
				var param = {
						  songName : $("#songName").val(),
						  singer : $("#singer").val(),
						  album : $("#album").val(),
						  fileName : $("#songFile").val(),
						  imageName : $("#imgFile").val(),
						  lyricName : $("#lyrFile").val()
						};
				$.post(url.NEW_SONG,param, function(data){
					if(data.code == 0){
						layer.msg("添加成功", {icon: 1, time: 1000}, function() {
							window.parent.location.reload();
						});
					}else if(data.code == 1) {
						layer.msg("系统出错", {icon: 2, time: 2000});
					}
				});
			};
	})
